package code.tools.behavior.achieve 
{
	import code.tools.behavior.IDisappear;
	import code.structure.manager.bitmap.YDisplayConvert;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	/**
	 * 缩放模式消失
	 * @author CYang
	 */
	public class DisappearBitmapScale implements IDisappear 
	{
		/**
		 * 面板引用
		 */
		private var _mPanel:Sprite;
		/**
		 * 面板出现后执行的操作
		 */
		private var _callBack:Function;
		
		private var bmContainer:Sprite;
		private var tempImage:Bitmap;
		/**
		 * 构造函数
		 * @param	_mPanel 面板
		 * @param	callback 执行函数
		 */
		public function DisappearBitmapScale(_mPanel:Sprite, callback:Function = null) 
		{
			this._mPanel = _mPanel;
			this._callBack = callback;
			bmContainer = new Sprite();
			bmContainer.mouseEnabled = bmContainer.mouseChildren = false;
			_mPanel.parent.addChild(bmContainer);
		}
		/**
		 * 消失
		 */
		public function disappear():void
		{
			_mPanel.visible = false;
			createTempBitmap();
			TweenLite.to(bmContainer, 0.5, { scaleX:0, scaleY:0, ease:Back.easeIn, onComplete:disComplete } );
		}
		
		private function disComplete():void{
			if(_callBack != null) _callBack();
			if(!tempImage) return;
			if(bmContainer.contains(tempImage)) bmContainer.removeChild(tempImage);
			tempImage = null;
		}
		private function createTempBitmap():void{
			if(!_mPanel) return;
			if(!tempImage) tempImage = new Bitmap(null,"auto",true);
			var arr:Array = YDisplayConvert.drawBitMapData(_mPanel);
			var bd:BitmapData = arr[0] as BitmapData;
			tempImage.bitmapData = bd;
			tempImage.smoothing = true;
			bmContainer.addChild(tempImage);
			//			tempImage.x = -tempImage.width*.5;
			//			tempImage.y = -tempImage.height*.5;
			//			bmContainer.x = tempImage.width*.5;
			//			bmContainer.y = tempImage.height*.5;
			var rect:Rectangle = _mPanel.getBounds(_mPanel);
			tempImage.x = rect.x;
			tempImage.y = rect.y;
			bmContainer.x = _mPanel.x;
			bmContainer.y = _mPanel.y;
			bmContainer.scaleX = bmContainer.scaleY = 1;
		}
	}
	
}